package com.um.jdy.mp.commission.contoller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.um.jdy.mp.commission.entity.dto.CashoutBankDTO;
import com.um.jdy.mp.commission.entity.dto.CashoutDTO;
import com.um.jdy.mp.commission.entity.vo.*;
import com.um.jdy.mp.commission.manager.CommissionManager;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.controller.BaseController;
import com.um.springboot.starter.orm.entity.BaseVO;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.params.CustomParams;
import com.um.springboot.starter.orm.params.PageParams;
import com.um.springboot.starter.orm.params.RangeParams;
import com.um.springboot.starter.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收益管理
 *
 * @author cs
 * 日期 2023/5/27
 * 版权所有 广州优匠科技有限公司
 */
@Api(tags = "收益管理")
@Validated
@RestController
@RequestMapping("/api/v1/cms/commission")
@SaCheckLogin
public class CommissionController extends BaseController<CommissionManager> {

    /**
     * 收益统计
     * @return
     */
    @ApiOperation(value = "收益统计")
    @GetMapping("statistics")
    public Response<StatisticsVO> statistics() {
        return Response.success(manager.getStatistics());
    }

    /**
     * 订单收益
     * @param month
     * @param page_no
     * @param page_size
     * @return
     */
    @ApiOperation(value = "订单收益")
    @GetMapping("/order/list")
    public Response<Page> orderList(@ApiParam(value = "月份") String month,
                                    @ApiParam(value = "页码") @NotNull Integer page_no,
                                    @ApiParam(value = "数据量") @NotNull Integer page_size) {
        return Response.success(manager.getOrderList(month, page_no, page_size));
    }

    /**
     * 可提现金额
     * @return
     */
    @ApiOperation(value = "可提现金额")
    @PostMapping("get_can_cashout")
    public Response<Map<String,Integer>> getCanCashout() {
        Map<String,Integer> map = new HashMap<>();
        map.put("commission",manager.getCanCashout());
        return Response.success(map);
    }

    /**
     * 提现记录
     * @param page_no
     * @param page_size
     * @param last_years
     * @param status
     * @param begin_date
     * @param end_date
     * @return
     */
    @ApiOperation(value = "提现记录")
    @GetMapping("cashout/page")
    public Response<Page<BaseVO>> page(@ApiParam(value = "页码") @NotNull Integer page_no,
                                       @ApiParam(value = "数据量") @NotNull Integer page_size,
                                       @ApiParam(value = "上个年月") String last_years,
                                       @ApiParam(value = "状态") String status,
                                       @ApiParam(value = "开始时间") String begin_date,
                                       @ApiParam(value = "结束时间") String end_date) {
        PageParams pageParams = new PageParams();
        pageParams.setPageNo(page_no);
        pageParams.setPageSize(page_size);
        if (!StringUtil.isEmpty(status)) {
            pageParams.getCondition().put("status", status);
        }

        if (!StringUtil.isEmpty(begin_date) && !StringUtil.isEmpty(end_date)) {
            RangeParams beginRangeParams = new RangeParams();
            beginRangeParams.setRange("begin");
            beginRangeParams.setValue(begin_date);
            RangeParams endRangeParams = new RangeParams();
            endRangeParams.setRange("end");
            endRangeParams.setValue(end_date);

            CustomParams customParams = new CustomParams();
            List<RangeParams> paramsList = new ArrayList<>();
            paramsList.add(beginRangeParams);
            paramsList.add(endRangeParams);
            Map<String, List<RangeParams>> rangeField = new HashMap<>();
            rangeField.put("DATE_FORMAT(create_time,'%Y-%m')",paramsList);
            customParams.setRangeField(rangeField);
            pageParams.setCustom(customParams);
        }
        return Response.success(manager.getCashoutPage(last_years,pageParams));
    }

    /**
     * 提现到银行卡
     * @param cashoutBankDTO
     * @return
     */
    @ApiOperation(value = "提现到银行卡")
    @PostMapping("cashout/bank")
    public Response cashoutBank(@RequestBody CashoutBankDTO cashoutBankDTO) {
        manager.cashoutBank(cashoutBankDTO);
        return Response.success();
    }

    /**
     * 提现到微信
     * @param cashoutWXDTO
     * @return
     */
    @ApiOperation(value = "提现到微信")
    @PostMapping("cashout/wx")
    public Response cashoutWX(@RequestBody CashoutDTO cashoutWXDTO) {
        manager.cashoutWX(cashoutWXDTO.getAmount());
        return Response.success();
    }


    /**
     * 流水统计
     * @return
     */
    @ApiOperation(value = "流水统计")
    @GetMapping("summary")
    public Response<CommissionSummaryVO> getCommissionSummary() {
        return Response.success(manager.getCommissionSummary());
    }

    /**
     * 代理商、运营商、商户流水查询
     * @return
     */
    @ApiOperation(value = "代理商、运营商、商户流水查询")
    @GetMapping("summary/list")
    public Response<CommissionListSummaryVO> getCommissionSummaryList(@ApiParam(value = "代理商、运营商、商户ID") @NotNull String id,
                                                                      @ApiParam(value = "类型：年份-year 月份-month") @NotNull String type,
                                                                      @ApiParam(value = "年份或月份") @NotNull String year_or_month) {
        return Response.success(manager.getCommissionSummaryList(id,type,year_or_month.trim()));
    }
}
